import base64
import requests
import URL as up

class acc_api:
    def __init__(self):
        self.headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        self.params = {'access_token': up.API}
        self.client = lambda URL, data: requests.post(URL, headers=self.headers, params=self.params, data=data)
        self.image = lambda path: base64.b64encode(open("image//" + path, 'rb').read()).decode()

    # 身份证识别
    def ocr_sfz(self):
        data = {'id_card_side': 'front', 'image': self.image("sfz.jpg")}
        for key, value in self.client(up.SFZ, data).json()['words_result'].items():
            print(f'{key} : {value["words"]}')


    # 人数识别
    def out_api(self):
        data = {'image': self.image("3.jpg")}
        return self.client(up.Body, data).json()['person_num']

    # 新闻播报
    def rest(self, text,name='新闻播报.mp3'):
        data = {
            "tex": text,
            "tok": up.TTS_TK,
            "cuid": "your_device_id_here",
            "ctp": "1",
            "lan": "zh",
            "per": "4"
        }
        with open(name, "wb") as f:
            f.write(self.client(up.TTS, data).content)

    # 图像文本转语音
    def out_ocr(self):
        s=''
        data = {'image': self.image("222.jpg")}
        text = self.client(up.OCR, data).json()
        for i in text['words_result']:
            s+=i['words']
        return s

    def Print(self):
        self.ocr_sfz()
        print(f'这张图片共计{self.out_api()}人')
        self.rest("中国国家广播电梯总台")
        print("新闻播报已生成")
        self.rest(text=self.out_ocr(), name='辅助阅读.mp3')
        print("辅助阅读已生成")

api = acc_api()
api.Print()